require 'View/UIBase/UIBase'

UIHeroLinkEnter = UIBase.New(PanelResNames.UIHeroLinkEnter)

function UIHeroLinkEnter.Init()
    UIBase.Init(UIHeroLinkEnter);
end

local limitLv = 0
-- 创建回调
function UIHeroLinkEnter:OnCreate()
    self._MAXNUM = 6;
    self.heroList = {};
    for i = 1, self._MAXNUM do
        table.insert(self.heroList, self:GetChild("btnType"..i));
        self:RegisterOnClick(self.heroList[i], function () self:OnGo(i) end);
    end
    self.linkLevel = self:GetChild("linkLevel");
    self.ctrlLevelUp = self:GetController("levelUp")
    self.limitCtrl = self:GetController("levelup")
    self.btnLevelUp = self:GetChild("btnLevelUp")
    self.List_ChooseHero = self:GetChild("List_ChooseHero")
    self.upList = self:GetList("Sv")
    self.topLevelLink = self:GetChild("levelLink")
    ---@type FairyGUI.GLoader3D
    self.effIdle = self:GetChild("efc_zhzl_lianzi_idle")
    ---@type FairyGUI.GLoader3D
    self.effStart = self:GetChild("efc_zhzl_lianzi_start")
    self.ctrlMax = self:GetController("levemax")
    self.tipCtrl = self:GetController("tipsshow")
    local cfg =  dataTable.getDataTableByStringId("t_global",388)
    local cfg2 = dataTable.getDataTableByStringId("t_global",389).f_string_value
    limitLv = cfg.f_int_value + logicMgr.HeroManager.GetHerosByStar(tonumber(cfg2[1])) *tonumber(cfg2[2])
    self:RegisterOnClick(self:GetChild("CloseBtn"),self.OnClose);
    self:RegisterOnClick(self:GetChild("BlackBg"),self.OnClose);
    self:RegisterOnClick(self:GetChild("help"),self.OnHelp);
    self:RegisterOnClick(self.btnLevelUp,self.ClickLevelUp)
    self:SetDynamicList(self.upList,self.ItemRender,self,true)
    self:RegisterEvent(Config.EventType.Fresh_Currency,self.RefreshLevelUp)
    self:RegisterEvent(Config.EventType.Refresh_HeroLinkUpgrade,self.Refresh_HeroLinkUpgradeMsg)
end
function UIHeroLinkEnter:RefreshLevelUpCost()
    local topLevel,topPhase = logicMgr.HeroLinkManager.GetTopLevel()
    local cfg = dataTable.getDataTableByStringId("t_characterexp",MakeTopLevelId(topLevel,topPhase))
    self.dataList = clone(decodeJsonStrToTabel(cfg.f_HeroCost))
    table.insert(self.dataList,{CurrencyEnum.HeroExp,cfg.f_HeroExp})
    self.costTipId = -1
    self.upList.numItems = #self.dataList
end
-- 显示
function UIHeroLinkEnter:OnEnable()
    self:Refresh();
    self.needShowHeadMenu = nil;
    if isOpenUI(PanelNames.UIHeadMenu) then
        self.needShowHeadMenu = true;
        closeUI(PanelNames.UIHeadMenu);
    end
    self.effStart.visible = false
    self.effIdle.visible = false
    self:RemoveAllTimeCall()
    if logicMgr.HeroLinkManager.IsTopLevelFirstReachNeedTip() then
        logicMgr.HeroLinkManager.SetTopLevelTipOver()
        self.effStart.visible = true
        local time = Util.PlayAction(self.effStart.wrapTarget,"start")
        self:TimeCall(self.PlayEffIdle,time*1000)
    else
        if logicMgr.HeroLinkManager.IsTopLevelOpen() then
            self:PlayEffIdle()
        end
    end
    if logicMgr.HeroLinkManager.IsTopLevelOpen() then
        if logicMgr.HeroLinkManager.GetTopLevel() >= limitLv then
            self.tipCtrl.selectedIndex = 0
            self.limitCtrl.selectedIndex = 1
        else
            self.limitCtrl.selectedIndex = 0
            self.tipCtrl.selectedIndex = 1
        end
        self.ctrlLevelUp.selectedIndex = 1
        self:RefreshLevelUp()
    else
        self.limitCtrl.selectedIndex = 0
        self.tipCtrl.selectedIndex = 1
        self.ctrlLevelUp.selectedIndex = 0
    end
end
function UIHeroLinkEnter:PlayEffIdle()
    self.effStart.visible = false
    self.effIdle.visible = true
    Util.PlayAction(self.effIdle.wrapTarget,"idle",true)
end
function UIHeroLinkEnter:Refresh()
    self:UpdateData();
end
function UIHeroLinkEnter:RefreshLevelUp()
    if logicMgr.HeroLinkManager.GetTopLevel() >= limitLv then
        self.tipCtrl.selectedIndex = 0
        self.limitCtrl.selectedIndex = 1
    else
        self.limitCtrl.selectedIndex = 0
        self.tipCtrl.selectedIndex = 1
    end
    local level = logicMgr.HeroLinkManager.GetTopLevel()
    local max = logicMgr.HeroLinkManager.GetMaxTopLevel()
    self.ctrlMax.selectedIndex = level >= max and 0 or 1
    self:RefreshLevelUpCost()
    --self.linkLevel.text = string.format(getLanguage("Level"), logicMgr.HeroLinkManager.GetTopLevel());
    self.topLevelLink.text = string.format(getLanguage("Level"), level)
end
function UIHeroLinkEnter:ItemRender(index,go)
    local data = self.dataList[index]
    local icon = go:GetChild("ItemIcon")
    local text = go:GetChild("ItemText")
    icon.url = logicMgr.ItemManager.GetItemIconPath(data[1])
    local own = ItemManager.getBagNumByTableID(data[1])
    local need = data[2]
    if own < need then
        text.text = string.TextColor(getNumString(own),"#FF0000") .."/"..getNumString(need)
        self.costTipId = self.costTipId < 0 and data[1] or self.costTipId
    else
        text.text = getNumString(own).."/"..getNumString(need)
    end

end

function UIHeroLinkEnter:UpdateData()
    self.minLevel = logicMgr.HeroLinkManager.GetHeroLevel();
    self.linkLevel.text = string.format(getLanguage("Level"), self.minLevel);
    self.level = self.minLevel;
    for i = 1, self._MAXNUM do
        --self.heroLinkInfo = logicMgr.HeroLinkManager.getLinkInfosByType(i);
        --if self.heroLinkInfo ~= nil then
        --    self.hero = logicMgr.HeroManager.getHeroDatabyPartnerId(self.heroLinkInfo.mainHeroId,true);
        --    if self.hero ~= nil then
        --        self.level = self.hero.level;
        --    end
        --end
        local levelText = self.heroList[i]:GetChild("level")
        if HeroLinkManager.IsTopLevelOpen() then
            levelText.text = string.format(getLanguage("Level"), HeroLinkManager.GetTopLevel());
        else
            levelText.text = string.format(getLanguage("Level"), self.level);
        end
        --if self.minLevel >= self.level then
        --    self.heroList[i]:GetController("minLevel").selectedIndex = 1;
        --else
        --    self.heroList[i]:GetController("minLevel").selectedIndex = 0;
        --end
        self.heroList[i]:GetController("minLevel").selectedIndex = 0;
        UIRedDots.addViewListen(PanelNames.UIHeroLinkEnter, self.heroList[i].name..i, self.heroList[i], Config.RedDotsType.HeroLink, i);
    end
    self.mainList = logicMgr.HeroLinkManager.getMainList()
    local ctrlItem = CtrlManager.GetCtrl(PanelNames.HeadRectItem);
    self.List_ChooseHero:RemoveChildrenToPool()
    for k, v in pairs(self.mainList) do
        ctrlItem.initData(self.List_ChooseHero:AddItemFromPool(), logicMgr.HeroManager.getHeroDatabyPartnerId(v, true));
    end
end

-- 详情
function UIHeroLinkEnter:OnGo(type)
    showUI(PanelNames.UICharactorInfoHeroLink, type);
end

-- 说明
function UIHeroLinkEnter:OnHelp()
    local data = { }
    if logicMgr.HeroLinkManager.IsTopLevelOpen() then
        data.title = "HelpTitle35";
        data.des = "HelpDes35";
    else
        data.title = "HelpTitle30";
        data.des = "HelpDes30";
    end
    showUI(PanelNames.CommonTips, data);
end

-- 关闭
function UIHeroLinkEnter:OnClose()
    self:OnBack();
    if self.needShowHeadMenu then
        showUI(PanelNames.UIHeadMenu);
        self.needShowHeadMenu = nil;
    end
end

-- 隐藏
function UIHeroLinkEnter:OnDisable()

end

-- 卸载
function UIHeroLinkEnter:OnDestroy()
    UIRedDots.removeViewEvent(PanelNames.UIHeroLinkEnter);
    self:RemoveTimeCall(self.PlayUpLevelEffDone)
end
function UIHeroLinkEnter:ClickLevelUp()
    if self.costTipId > 0 then
        UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),ItemManager.GetItemName(self.costTipId)))
        return
    end
    if logicMgr.HeroLinkManager.GetTopLevel() >= limitLv then
        UISysTips.AddMsg("等级达到上限")
        return
    end
    --[[logicMgr.HeroLinkManager.topLevel = HeroLinkManager.GetTopLevel() + 1
    for k,v in pairs(self.dataList) do
        local own = ItemManager.getBagNumByTableID(v[1])
        ItemManager.setCurrencyCount(v[1],math.max(0,own -v[2]),-1,"")
    end]]

    --FireEvent(Config.EventType.Fresh_HeroLinkUpgrade)
    heroLinkHandler.ReqLinkPeakUpgrade()
end
function UIHeroLinkEnter:PlayUpLevelEffDone()
    self.effStart.visible = false
end
function UIHeroLinkEnter:Refresh_HeroLinkUpgradeMsg()
    self:RefreshLevelUp()
    self.effStart.visible = true
    self:RemoveTimeCall(self.PlayUpLevelEffDone)
    local time = Util.PlayAction(self.effStart.wrapTarget,"start")
    self.timerId = self:TimeCall(self.PlayUpLevelEffDone,time*1000)
    self:Refresh()
end
return UIHeroLinkEnter